rm(list = ls())
library(data.table)
library(plyr)
library(tidyr)
library(lfe)
library(stargazer)
library(xtable)
library(sandwich)
library(roll)
library(readxl)
library(readr)
library(zoo)
library(texreg)
library(DescTools)
library(ggplot2)
library(gtools)

m <- data.table(read_xlsx("../Data/MasterData.xlsx", skip = 1, guess_max = 1e4))
m[, age := 2021 - year]

################################################################
# Binscatter
################################################################

m[, retrecall_1day_rate := retrecall_1day_rate * 100]
m[, retrecall_30day_rate := retrecall_30day_rate * 100]
m[, retrecall_1year_rate := retrecall_1year_rate * 100]
m[, retrecall_5year_rate := retrecall_5year_rate * 100]


m[, expret30day_market_rate := expret30day_market_rate * 100]
m[, expret1year_market_rate := expret1year_market_rate * 100]
m[, expret30day_self_rate := expret30day_self_rate * 100]
m[, expret1year_self_rate := expret1year_self_rate * 100]

m[, memrableperiod_pret := memrableperiod_pret * 100]
m[, memrableperiod_aret := memrableperiod_aret * 100]
m[, bias := (memrableperiod_pret - memrableperiod_aret)]

m[, memrableperiod_begin := as.yearmon(memrableperiod_begin, "%Ym%m")]
m[, memrableperiod_end := as.yearmon(memrableperiod_end, "%Ym%m")]
m[, memrableperiod_dist := as.yearmon(2022 + 2 / 12) - (memrableperiod_begin + memrableperiod_end) / 2]

m <- m[!is.na(memrableperiod_pret)]

m[, age.bin := quantcut(age, 20)]

ggplot(
  m[, .(age = mean(age), stats = mean(memrableperiod_pret, na.rm = T)), by = .(age.bin)],
  aes(x = age, y = stats)
) +
  geom_point(colour = "black", size = 2) +
  geom_smooth(method = lm, se = F) +
  ylab("\U211d[MktRet episode]") +
  xlab("Age") +
  theme(legend.position = "none") + theme_minimal()
ggsave("../Figures/pic-binscatter-age.pdf", width = 6, height = 4)

ggplot(
  m[, .(age = mean(age), stats = mean(memrableperiod_aret, na.rm = T)), by = .(age.bin)],
  aes(x = age, y = stats)
) +
  geom_point(colour = "black", size = 2) +
  geom_smooth(method = lm, se = F) +
  ylab("Actual Return") +
  xlab("Age") +
  theme(legend.position = "none") +
  theme_minimal()
ggsave("../Figures/pic-binscatter-age-actual.pdf", width = 6, height = 4)

m[, age.bin := quantcut(age, 20)]
ggplot(
  m[, .(age = mean(age), stats = mean(memrableperiod_pret - memrableperiod_aret, na.rm = T)), by = .(age.bin)],
  aes(x = age, y = stats)
) +
  geom_point(colour = "black", size = 2) +
  geom_smooth(method = lm, se = F) +
  ylab("Bias") +
  xlab("Age") +
  theme(legend.position = "none") +
  theme_minimal()
ggsave("../Figures/pic-binscatter-age-bias.pdf", width = 6, height = 4)
